﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ajayumi.develop.win32.Transport.AppUpgrade.Controls
{
    /// <summary>
    /// 更新描述
    /// Author   : 黄乙冬
    /// Date     : 2011-9-1 08:45:00
    /// </summary>
    public partial class UcUpgradeDetail : UcBase
    {
        private bool m_IsRestore = false;

        public UcUpgradeDetail()
        {
            InitializeComponent();
        }

        public UcUpgradeDetail(UcBase preControl)
            : base(preControl)
        {
            InitializeComponent();
        }

        protected override void InitDefaultValue()
        {
            base.InitDefaultValue();

            this.Title = "更新日志";
        }

        internal override UcBase Next()
        {
            UcBase uc = null;
            if (this.m_IsRestore)
            { uc = new UcRestoring(this); }
            else
            { uc = new UcDownloading(this); }
            return uc;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Utility.HasBackup())
            {
                this.SetButtonVisible(WizardFormButtons.Restore | WizardFormButtons.Upgrade | WizardFormButtons.Exit);
            }
            else
            { this.SetButtonVisible(WizardFormButtons.Upgrade | WizardFormButtons.Exit); }

            this.backgroundWorker1.RunWorkerAsync();
        }

        internal override void Restore()
        {
            this.m_IsRestore = true;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                using (UpgradeServiceClient client = new UpgradeServiceClient())
                {
                    Program.UpgradeInfo = client.GetUpgrade(ArgumentSerialization.AppName, ArgumentSerialization.AppVersion);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                e.Cancel = true;
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.txtDescription.Text = Utility.FormatDescription(Program.UpgradeInfo.Description);
            if (ArgumentSerialization.IsSilent)
            {
                this.PerformNext();
            }

        }

    }
}
